package servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 根据outputStream输出流进行Response对象的封装
 * 该对象需要提供核心方法，输出html
 */
public class Response {


    private OutputStream outputStream; //根据输出流进行Response对象的封装

    public Response() {

    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }


    public void outPut(String context) throws IOException {
        outputStream.write(context.getBytes());
    }

    public void outputHtml(String path) throws IOException {
        //获取静态资源文件的绝对路径
        String absoluteResourcePath = StaticResourceUtil.getAbsoluteResourcePath(path);

        //输出静态资源文件
        File file = new File(absoluteResourcePath);
        if(file.exists() && file.isFile()){
            StaticResourceUtil.outputStatcResource(new FileInputStream(file),outputStream);
        }else {
            outPut(HttpProtocolUtil.getResponseHeader404());
        }


    }
}
